import { rgb, toRGBString } from "./chroma.replace";

/** 混合颜色的算法,两个 alpha 不能同时为 0  */
export const blendColor = (
    color1: u32,
    a1: u32,
    color2: u32,
    A2: u32
): Uint32Array => {
    const rgb1 = rgb(color1);
    const R1 = rgb1[0];
    const G1 = rgb1[1];
    const B1 = rgb1[2];

    const rgb2 = rgb(color2);
    const R2 = rgb2[0];
    const G2 = rgb2[1];
    const B2 = rgb2[2];

    // https://blog.csdn.net/aa13058219642/article/details/80953341

    const a2 = A2 - (a1 * A2) / 255;
    const a = a1 + a2;

    const R = (a1 * R1 + a2 * R2) / a;
    const G = (a1 * G1 + a2 * G2) / a;
    const B = (a1 * B1 + a2 * B2) / a;

    const newOne = toRGBString(u32(R), u32(G), u32(B));
    const arr = new Uint32Array(2);
    arr[0] = newOne;
    arr[1] = u32(a);
    return arr;
};
